package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemOrderDetailsFollowBinding;
import com.example.yichuangyun.databinding.ItemParentBinding;
import com.example.yichuangyun.model.order.OrderDetailAllFollowModel;
import com.example.yichuangyun.model.order.OrderFollowDataModel;
import com.example.yichuangyun.model.order.RealNameModel;
import com.example.yichuangyun.utils.CommonUtil;

public class ParentOrderFollowAdapter extends BaseQuickAdapter<OrderDetailAllFollowModel, DataBindingHolder<ItemParentBinding>> {
    private final Context context;
    private static OnCallListener onCallListener;

    public ParentOrderFollowAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemParentBinding> itemParentBindingDataBindingHolder, int i, @Nullable OrderDetailAllFollowModel orderDetailAllFollowModel) {
        ItemParentBinding itemParentBinding = itemParentBindingDataBindingHolder.getBinding();
        if (orderDetailAllFollowModel != null) {
            switch (i) {
                case 0:
                    OrderFollowAdapter orderFollowAdapter = new OrderFollowAdapter(0, context);
                    itemParentBinding.recyclerView.setAdapter(orderFollowAdapter);
                    orderFollowAdapter.submitList(orderDetailAllFollowModel.getOrderFollowList());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailAllFollowModel.getOrderFollowList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailAllFollowModel.getOrderFollowList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailAllFollowModel.getOrderFollowList().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailAllFollowModel.getOrderFollowList().isEmpty());
                    break;
                case 1:
                    OrderFollowAdapter serviceFollowAdapter = new OrderFollowAdapter(1, context);
                    itemParentBinding.recyclerView.setAdapter(serviceFollowAdapter);
                    serviceFollowAdapter.submitList(orderDetailAllFollowModel.getTrackList());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailAllFollowModel.getTrackList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailAllFollowModel.getTrackList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailAllFollowModel.getTrackList().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailAllFollowModel.getTrackList().isEmpty());
                    break;
                case 2:
                    OrderFollowAdapter customVisitAdapter = new OrderFollowAdapter(2, context);
                    itemParentBinding.recyclerView.setAdapter(customVisitAdapter);
                    customVisitAdapter.submitList(orderDetailAllFollowModel.getVisitList());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailAllFollowModel.getVisitList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailAllFollowModel.getVisitList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailAllFollowModel.getVisitList().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailAllFollowModel.getVisitList().isEmpty());
                    break;
            }
            itemParentBinding.recyclerView.setVisibility(orderDetailAllFollowModel.isOpen() ? View.VISIBLE : View.GONE);
            itemParentBinding.tvTitle.setText(orderDetailAllFollowModel.getTitle());
            itemParentBinding.ivArrow.setImageResource(orderDetailAllFollowModel.isOpen() ? R.mipmap.icon_down_blue : R.mipmap.icon_up_blue);
            itemParentBinding.tvExpand.setText(orderDetailAllFollowModel.isOpen() ? context.getString(R.string.no_expand) : context.getString(R.string.expand));

            itemParentBinding.llTitle.setOnClickListener(view -> {
                orderDetailAllFollowModel.setOpen(!orderDetailAllFollowModel.isOpen());
                notifyItemChanged(itemParentBindingDataBindingHolder.getAbsoluteAdapterPosition());
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemParentBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_parent, viewGroup);
    }

    public void setOnCallListener(OnCallListener mOnCallListener) {
        onCallListener = mOnCallListener;
    }

    public interface OnCallListener {
        void onCall(String userName);
    }

    private static class OrderFollowAdapter extends BaseQuickAdapter<OrderFollowDataModel.OrderFollowModel, DataBindingHolder<ItemOrderDetailsFollowBinding>> {

        private final int flag;
        private final Context context;

        public OrderFollowAdapter(int flag, Context context) {
            this.flag = flag;
            this.context = context;
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemOrderDetailsFollowBinding> itemOrderDetailsFollowBindingDataBindingHolder, int i, @Nullable OrderFollowDataModel.OrderFollowModel orderFollowModel) {
            ItemOrderDetailsFollowBinding binding = itemOrderDetailsFollowBindingDataBindingHolder.getBinding();
            if (orderFollowModel != null) {
                switch (flag) {
                    case 0:
                        binding.ll.setVisibility(View.GONE);
                        binding.tvContent.setText(CommonUtil.checkText(orderFollowModel.getDetail()));
                        binding.tvText1.setText(context.getString(R.string.recorder_mao));
                        binding.tvText2.setText(context.getString(R.string.reminder_mao));
                        binding.tvName1.setText(CommonUtil.setText(orderFollowModel.getTrackUserName()));
                        if (orderFollowModel.getRemindList() != null && !orderFollowModel.getRemindList().isEmpty()) {
                            StringBuilder builder = new StringBuilder();
                            for (RealNameModel remindModel : orderFollowModel.getRemindList()) {
                                builder.append(remindModel.getRealName()).append("，");
                            }
                            if (builder.toString().endsWith("，")) {
                                binding.tvName2.setText(builder.substring(0, builder.toString().length() - 1));
                            }
                        }
                        if (orderFollowModel.getAttachmentList() != null && !orderFollowModel.getAttachmentList().isEmpty()) {
                            binding.llAttachment.setVisibility(View.VISIBLE);
                            FileNameAdapter fileNameAdapter = new FileNameAdapter(context);
                            binding.recyclerView.setAdapter(fileNameAdapter);
                            fileNameAdapter.submitList(orderFollowModel.getAttachmentList());
                        } else {
                            binding.llAttachment.setVisibility(View.GONE);
                        }
                        break;
                    case 1:
                        binding.ll.setVisibility(View.VISIBLE);
                        binding.tvContent.setText(CommonUtil.checkText(orderFollowModel.getDetail()));
                        binding.tvText1.setText(context.getString(R.string.service_name_mao));
                        binding.tvText2.setText(context.getString(R.string.contact_object_mao));
                        binding.tvText3.setText(context.getString(R.string.recorder_mao));
                        binding.tvText4.setText(context.getString(R.string.reminder_mao));
                        binding.tvName1.setText(CommonUtil.setText(orderFollowModel.getServiceName()));
                        binding.tvName2.setText(Html.fromHtml(context.getString(R.string.visit_person_and_phone, CommonUtil.checkText(orderFollowModel.getContact()), CommonUtil.getStarPhone(orderFollowModel.getPhone())), Html.FROM_HTML_MODE_LEGACY));
                        binding.tvName2.setOnClickListener(view -> {
                            if (onCallListener != null) {
                                onCallListener.onCall(orderFollowModel.getContact());
                            }
                        });
                        binding.tvName3.setText(CommonUtil.setText(orderFollowModel.getTrackUserName()));
                        if (orderFollowModel.getRemindList() != null && !orderFollowModel.getRemindList().isEmpty()) {
                            StringBuilder builder = new StringBuilder();
                            for (RealNameModel remindModel : orderFollowModel.getRemindList()) {
                                builder.append(remindModel.getRealName()).append("，");
                            }
                            if (builder.toString().endsWith("，")) {
                                binding.tvName4.setText(builder.substring(0, builder.toString().length() - 1));
                            }
                        }
                        if (orderFollowModel.getAttachmentList() != null && !orderFollowModel.getAttachmentList().isEmpty()) {
                            binding.llAttachment.setVisibility(View.VISIBLE);
                            FileNameAdapter fileNameAdapter = new FileNameAdapter(context);
                            binding.recyclerView.setAdapter(fileNameAdapter);
                            fileNameAdapter.submitList(orderFollowModel.getAttachmentList());
                        } else {
                            binding.llAttachment.setVisibility(View.GONE);
                        }
                        break;
                    case 2:
                        binding.ll.setVisibility(View.GONE);
                        binding.llAttachment.setVisibility(View.GONE);
                        binding.tvContent.setText(CommonUtil.checkText(orderFollowModel.getRemark()));
                        binding.tvText1.setText(context.getString(R.string.visit_object_mao));
                        binding.tvText2.setText(context.getString(R.string.visit_person_mao));
                        binding.tvName1.setText(Html.fromHtml(context.getString(R.string.visit_person_and_phone, orderFollowModel.getCustomerName(), CommonUtil.getStarPhone(orderFollowModel.getTelNumber())), Html.FROM_HTML_MODE_LEGACY));
                        binding.tvName2.setText(orderFollowModel.getCreateBy());
                        binding.tvName1.setOnClickListener(view -> {
                            if (onCallListener != null) {
                                onCallListener.onCall(orderFollowModel.getCustomerName());
                            }
                        });
                        break;
                }
                binding.tvCreateTime.setText(orderFollowModel.getCreateTime());
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemOrderDetailsFollowBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_order_details_follow, viewGroup);
        }
    }

}
